﻿Public Class frmUserPermission
    Dim dbBase As New dbUser
    Dim frmCtrl As New FormControl
    Dim Var As New varDeclare
    Dim dsBase As DataSet
    Dim usrGenID As String
    Dim EntryID As String

    Private Sub frmUserPermission_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        EntryID = ""
        GridAndFind1.Enabled = True
        dsBase = dbBase.UserList(Var.CompID)
        ToolbarHeader1.Iniliaze("CONTROL : User Permission", Me, Nothing, GridAndFind1.getDataGridView(), "User ID")
        GridAndFind1.Initialize(dsBase, "User ID", ToolbarHeader1, Me, False)
        If (dsBase IsNot Nothing) Then
            GridAndFind1.getDataGridView().Columns("User Password").Visible = False
            GridAndFind1.getDataGridView().Columns("User Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End If
        GridAndFind1.Enabled = True
        gbSetPermission.Enabled = False
        ToolbarHeader1.ActionCtrl(False, False, True, False, False, False, False, True, True)
        cmbGroupPermission.SelectedIndex = 0
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String) Handles GridAndFind1.GridCellEnter
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[User ID] = '" & KeyValue & "'"
        EntryID = dvFind.Item(0).Row("User ID").ToString()
        GetPermission(EntryID)
        ToolbarHeader1.ActionCtrl(False, False, True, False, False, False, False, True, True)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsEditClick
        GridAndFind1.Enabled = False
        gbSetPermission.Enabled = True
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmUserPermission, Me.ParentForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsCancelClick
        frmCtrl.openChild(New frmUserPermission, Me.ParentForm)
    End Sub

    Private Sub tsSave_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsSaveClick
        Dim dtResult As New DataTable
        dtResult.Columns.Add("frmGenID")
        dtResult.Columns.Add("perOpen")
        dtResult.Columns.Add("perInsert")
        dtResult.Columns.Add("perUpdate")
        dtResult.Columns.Add("perDelete")
        dtResult.Columns.Add("perPrint")
        Dim rowCount As Integer = dgPermission.Rows.Count
        For i = 0 To rowCount - 1
            Try
                dtResult.Rows.Add(dgPermission.Rows(i).Cells(0).Value, _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(3).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(4).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(5).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(6).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(7).Value))
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Next

        Dim result As Boolean = dbBase.SetUserPermission(usrGenID, dtResult)
        If (result) Then
            frmCtrl.openChild(New frmUserPermission, Me.ParentForm)
        Else : MessageBox.Show("Error in Save Progress")
        End If
    End Sub

    Private Sub GetPermission(UserID As String)
        Dim dtPermission As DataTable = dbBase.GetUserLoginDetail(UserID).Tables(1)
        usrGenID = dbBase.GetUserLoginDetail(UserID).Tables(0).Rows(0)(0).ToString()
        Dim rowCount As Integer = dtPermission.Rows.Count
        dgPermission.Rows.Clear()
        If (rowCount > 0) Then
            dgPermission.Rows.Add(rowCount)
            For i = 0 To rowCount - 1
                Try
                    dgPermission.Rows(i).Cells(0).Value = dtPermission.Rows(i)(1).ToString().Trim()
                    dgPermission.Rows(i).Cells(1).Value = dtPermission.Rows(i)(0).ToString().Trim()
                    dgPermission.Rows(i).Cells(2).Value = dtPermission.Rows(i)(2).ToString().Trim()
                    dgPermission.Rows(i).Cells(3).Value = CBool(CInt(dtPermission.Rows(i)(3)))
                    dgPermission.Rows(i).Cells(4).Value = CBool(CInt(dtPermission.Rows(i)(4)))
                    dgPermission.Rows(i).Cells(5).Value = CBool(CInt(dtPermission.Rows(i)(5)))
                    dgPermission.Rows(i).Cells(6).Value = CBool(CInt(dtPermission.Rows(i)(6)))
                    dgPermission.Rows(i).Cells(7).Value = CBool(CInt(dtPermission.Rows(i)(7)))
                Catch ex As Exception
                End Try
            Next
        End If
    End Sub

    Private Sub cmbGroupPermission_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbGroupPermission.SelectedIndexChanged
        Dim rowCount As Integer = dgPermission.Rows.Count
        Select Case cmbGroupPermission.SelectedItem.ToString
            Case "Administrator"
                For i = 0 To rowCount - 1
                    Try
                        dgPermission.Rows(i).Cells(3).Value = True
                        dgPermission.Rows(i).Cells(4).Value = True
                        dgPermission.Rows(i).Cells(5).Value = True
                        dgPermission.Rows(i).Cells(6).Value = True
                        dgPermission.Rows(i).Cells(7).Value = True
                    Catch ex As Exception
                    End Try
                Next
            Case "Custom"

            Case Else
                For i = 0 To rowCount - 1
                    Try
                        dgPermission.Rows(i).Cells(3).Value = False
                        dgPermission.Rows(i).Cells(4).Value = False
                        dgPermission.Rows(i).Cells(5).Value = False
                        dgPermission.Rows(i).Cells(6).Value = False
                        dgPermission.Rows(i).Cells(7).Value = False
                        If (InStr(dgPermission.Rows(i).Cells(1).Value, cmbGroupPermission.SelectedItem.ToString) > 0) Then
                            dgPermission.Rows(i).Cells(3).Value = True
                            dgPermission.Rows(i).Cells(4).Value = True
                            dgPermission.Rows(i).Cells(5).Value = True
                            dgPermission.Rows(i).Cells(6).Value = True
                            dgPermission.Rows(i).Cells(7).Value = True
                        End If
                    Catch ex As Exception
                    End Try
                Next
        End Select
    End Sub

    Private Sub dgPermission_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgPermission.CellContentClick
        If (e.RowIndex > -1 And e.ColumnIndex > 2) Then
            cmbGroupPermission.SelectedIndex = 0
        End If
    End Sub
End Class